package com.xinxing.learning.batch.config;

import com.baomidou.mybatisplus.annotation.DbType;
import com.baomidou.mybatisplus.core.MybatisConfiguration;
import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.OptimisticLockerInnerInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor;
import com.baomidou.mybatisplus.extension.spring.MybatisSqlSessionFactoryBean;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.jdbc.DataSourceBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.DependsOn;
import org.springframework.context.annotation.Primary;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;

import javax.sql.DataSource;

/**
 * 多数据源 如何使用mybatis-plus的分页插件
 * https://blog.csdn.net/mvcVSstruts/article/details/121540055
 */
@Configuration
@MapperScan(basePackages = "com.xinxing.learning.batch.mapper.batch", sqlSessionFactoryRef = "batchSqlSessionFactory")
public class BatchDataSourceConfig {

    @Bean("datasource")
    @ConfigurationProperties(prefix = "spring.datasource")
    @Primary
    public DataSource batchDataSource() {
        return DataSourceBuilder.create().build();
    }

    @Bean("batchSqlSessionFactory")
    @DependsOn("datasource")
    @Primary
    public SqlSessionFactory sessionFactory(@Qualifier("datasource") DataSource dataSource) throws Exception {
        MybatisSqlSessionFactoryBean mybatisSqlSessionFactoryBean = new MybatisSqlSessionFactoryBean();
        mybatisSqlSessionFactoryBean.setDataSource(dataSource);
        mybatisSqlSessionFactoryBean.setTypeAliasesPackage("com.xinxing.learning.batch.entity");
        PathMatchingResourcePatternResolver resourcePatternResolver = new PathMatchingResourcePatternResolver();
        String localMapperXmlPattern = "classpath*:/mapper/batch/*.xml";
        mybatisSqlSessionFactoryBean.setMapperLocations(resourcePatternResolver.getResources(localMapperXmlPattern));
        MybatisConfiguration configuration = new MybatisConfiguration();
        // configuration.setLogImpl(StdOutImpl.class);
        configuration.setMapUnderscoreToCamelCase(true);
        mybatisSqlSessionFactoryBean.setConfiguration(configuration);
        mybatisSqlSessionFactoryBean.setPlugins(batchMybatisPlusInterceptor());
        return mybatisSqlSessionFactoryBean.getObject();
    }

    @Bean("batchMybatisPlusInterceptor")
    public MybatisPlusInterceptor batchMybatisPlusInterceptor() {
        MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
        // 分页插件
        PaginationInnerInterceptor paginationInnerInterceptor = new PaginationInnerInterceptor();
        paginationInnerInterceptor.setDbType(DbType.MYSQL);
        paginationInnerInterceptor.setOverflow(true);
        interceptor.addInnerInterceptor(paginationInnerInterceptor);
        // 版本号实现乐观锁插件
        interceptor.addInnerInterceptor(new OptimisticLockerInnerInterceptor());
        return interceptor;
    }
}
